#include<stdio.h>


int compare(const void *p1, const void *p2)
{
	return *(int*)p1 - *(int*)p2;
}

void swap(char* buf1, char* buf2, int size)
{
	int i = 0;
	char temp;
	for(i = 0; i < size; i++)
	{
		temp = *buf1;
		*buf1 = *buf2;
		*buf2 = temp;
		buf1++;
		buf2++;
	}
} 

void bubble_sort(void* base, int num, int size, int(*compare)(const void*, const void*))
{
	int i = 0, j = 0;
	for(i = 0; i < num - 1; i ++)
	{
		for(j = 0; j < num - i - 1; j++)
		{
			if(compare((char*)base + j*size, (char*)base + (j+1)*size) > 0)
			{
				swap((char*)base + j*size, (char*)base + (j+1)*size, size);
			}
		}
	}
}

int main()
{
	int arr[9] = {1,4,5,2,7,3,9,6,8};
	int num = sizeof(arr) / sizeof(arr[0]);

	bubble_sort(arr, num, sizeof(int), compare);
	
	int i;
	for(i = 0; i < num; i++)
	{
		printf("%d ", arr[i]);
	}
}
